/**
 * 订单相关接口-RestFull风格
 * 包括删除订单、查询订单
 */

// 引入express
const express = require("express")
// 引入数据库连接池
const pool = require("../../db/db.js")
// 引入封装后的响应结果
const Response = require("../../utils/ResponseData.js")
// 引入表单验证模块joi
const joi = require("joi")

// 创建路由
const router = express.Router()

// 订单查询接口
/**
 * 请求url:
 */
router.get("/petOrder/get/:page/:pageSize/:keywords", async (req, res) => {
    let { page, pageSize, keywords } = req.params
    let schema = joi.object({
        page: joi.number().required(),
        pageSize: joi.number().max(10).required(),
        keywords: joi.string().required()
    })
    let { error } = schema.validate(req.params)
    if (error) {
        res.send(Response.error(error))
        return
    }
    try {
        const startIndex = (page - 1) * pageSize
        pageSize = parseInt(pageSize)
        if (keywords == "''") keywords = ''
        const sql1 = `select 
                    po.po_id,
                    po.po_number,
                    p.p_img,
                    p.p_name,
                    u.u_name,
                    c.name,
                    c.tel,
                    b.b_name,
                    b.b_city_detail,
                    po.po_time,
                    po.po_price,
                    po.po_status 
                from
                    pet_po po,
                    pet p,
                    contact c,
                    business b,
                    user u 
                where po.pet_id = p.p_id 
                    and po.bus_id = b.b_id 
                    and c.user_id = u.u_id 
                    and p.user_id = u.u_id 
                    and (
                    c.name like ? 
                    or c.tel like ?)
                limit ?,?`
        const result1 = await pool.querySync(sql1, [`%${keywords}%`, `%${keywords}%`, startIndex, pageSize])
        const sql2 = `select 
                    count(po_id)
                    as
                    count
                from
                    (select 
                    po.po_id,
                    po.po_number,
                    p.p_img,
                    p.p_name,
                    u.u_name,
                    c.name,
                    c.tel,
                    b.b_name,
                    b.b_city_detail,
                    po.po_time,
                    po.po_price,
                    po.po_status 
                    from
                    pet_po po,
                    pet p,
                    contact c,
                    business b,
                    user u 
                    where po.pet_id = p.p_id 
                    and po.bus_id = b.b_id 
                    and c.user_id = u.u_id 
                    and p.user_id = u.u_id 
                    and (
                        c.name like ? 
                        or c.tel like ?
                    )
                )p1`
        const result2 = await pool.querySync(sql2, [`%${keywords}%`, `%${keywords}%`])
        // 总条数
        const total = result2[0].count
        // 总页数=总条数%pageSize==0?总条数/pageSize:总条数/pageSize+1
        // 总页数=Math.ceil(总条数/pageSize)
        const maxPage = Math.ceil(total / pageSize)
        res.send(Response.success({
            page,
            pageSize,
            total,
            maxPage,
            result1
        }))
    } catch (e) {
        res.send(Response.error(e))
    }
})

// 删除日志
/**
 * 
 */
router.delete("/petOrder/delete/:id", (req, res) => {
    let { id } = req.params
    let schema = joi.object({ id: joi.number().required() })
    let { error } = schema.validate(req.params)
    if (error) {
        res.send(Response.error(error))
        console.log(error);
        return
    }
    const sql = "delete from pet_po where po_id = ?"
    pool.query(sql, [id], (err, result) => {
        console.log(result);
        if (err) {
            res.send(Response.error("删除失败!", 500))
        }
        if (result && result.affectedRows > 0) {
            res.send(Response.success(null, "删除成功!"))
        } else {
            res.send(Response.error("删除失败!", 500))
        }



    })
})

module.exports = router